package cn.enzo.yun.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.enzo.utils.controller.CommonController;
import cn.enzo.yun.domain.User;
import cn.enzo.yun.service.IUserService;

@Controller
@RequestMapping(value=UserController.url_path)
public class UserController extends CommonController<User>{
	
	public final static String url_path = "/user";
	
	private final static Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="list")
	public ModelAndView list(ModelAndView mav,User entity,@RequestParam(defaultValue ="1") Integer pageNum) throws Exception{
		PageHelper.startPage(pageNum, 10);
		List<User> queryByCondition = userService.queryByCondition(entity);
		PageInfo<User> pageInfo = new PageInfo<>(queryByCondition);
		mav.addObject(PAGE, pageInfo);
		mav.setViewName("user/userlist");
		return mav;
	}
	
	@RequestMapping(value="delete")
	public ModelAndView delete(ModelAndView mav,@RequestParam(defaultValue = "1")Integer pageNum,String id){
		logger.info("store delete ...");
		try {
			userService.deleteBykey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		mav.setViewName(REDIRECT + url_path + "/list?pageNum=" + pageNum);
		return mav;
	}

}
